// config

var app =
    angular.module('app')
    .config(
        ['$controllerProvider', '$compileProvider', '$filterProvider', '$provide', 'ngDialogProvider',
            function($controllerProvider, $compileProvider, $filterProvider, $provide, ngDialogProvider) {
                // lazy controller, directive and service
                app.controller = $controllerProvider.register;
                app.directive = $compileProvider.directive;
                app.filter = $filterProvider.register;
                app.factory = $provide.factory;
                app.service = $provide.service;
                app.constant = $provide.constant;
                app.value = $provide.value;
                window.addEditHouseType = {}; 
                window.addEditHouseType.type = 'add'; 
                ngDialogProvider.setDefaults({
                    className: 'ngdialog-theme-plain',
                    plain: false,
                    showClose: true,
                    closeByDocument: true,
                    closeByEscape: true,
                    appendTo: false,
                    preCloseCallback: function() {
                        console.log('default pre-close callback');
                    }
                });
            }
        ])
    .config(function($httpProvider) {
        $httpProvider.interceptors.push('UserInterceptor');
        $httpProvider.interceptors.push('preventRepeatSubmit');
    });

app.factory('UserInterceptor', ["$q", "$rootScope", "$localStorage", function($q, $rootScope, $localStorage) {
    return {
        request: function(config) {
            var user = $localStorage.user;
            $rootScope.userPhone =  $localStorage.user;
            // console.log("config.url:::",config.url)

            if (!!user && !!user.token) {
                $rootScope.user = user;
                config.headers.token = user.token;
            }

            return config;
        },
        responseError: function(response) {
            var data = response.data;
            // 判断错误码，如果是未登录
            if (data["errorCode"] == "500999") {
                // 清空用户本地token存储的信息，如果
                $rootScope.user = { token: "" };
                // 全局事件，方便其他view获取该事件，并给以相应的提示或处理
                $rootScope.$emit("userIntercepted", "notLogin", response);
            }
            // 如果是登录超时
            if (data["errorCode"] == "500998") {
                $rootScope.$emit("userIntercepted", "sessionOut", response);
            }
            return $q.reject(response);
        }
    };
}]);

app.factory('uploadConf', function(){  
    
    return {};});
app.factory('uploadConf2', function(){  
    
    return {};});
